Conditions | 39 |
Total Lines | 101 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like jquery.mousewheel.js ➔ handler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*! Copyright (c) 2013 Brandon Aaron (http://brandon.aaron.sh) |
||
84 | function handler(event) { |
||
85 | var orgEvent = event || window.event, |
||
86 | args = slice.call(arguments, 1), |
||
87 | delta = 0, |
||
88 | deltaX = 0, |
||
89 | deltaY = 0, |
||
90 | absDelta = 0; |
||
91 | event = $.event.fix(orgEvent); |
||
92 | event.type = 'mousewheel'; |
||
93 | |||
94 | // Old school scrollwheel delta |
||
95 | if ( 'detail' in orgEvent ) { deltaY = orgEvent.detail * -1; } |
||
96 | if ( 'wheelDelta' in orgEvent ) { deltaY = orgEvent.wheelDelta; } |
||
97 | if ( 'wheelDeltaY' in orgEvent ) { deltaY = orgEvent.wheelDeltaY; } |
||
98 | if ( 'wheelDeltaX' in orgEvent ) { deltaX = orgEvent.wheelDeltaX * -1; } |
||
99 | |||
100 | // Firefox < 17 horizontal scrolling related to DOMMouseScroll event |
||
101 | if ( 'axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) { |
||
102 | deltaX = deltaY * -1; |
||
103 | deltaY = 0; |
||
104 | } |
||
105 | |||
106 | // Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatabilitiy |
||
107 | delta = deltaY === 0 ? deltaX : deltaY; |
||
108 | |||
109 | // New school wheel delta (wheel event) |
||
110 | if ( 'deltaY' in orgEvent ) { |
||
111 | deltaY = orgEvent.deltaY * -1; |
||
112 | delta = deltaY; |
||
113 | } |
||
114 | if ( 'deltaX' in orgEvent ) { |
||
115 | deltaX = orgEvent.deltaX; |
||
116 | if ( deltaY === 0 ) { delta = deltaX * -1; } |
||
117 | } |
||
118 | |||
119 | // No change actually happened, no reason to go any further |
||
120 | if ( deltaY === 0 && deltaX === 0 ) { return; } |
||
121 | |||
122 | // Need to convert lines and pages to pixels if we aren't already in pixels |
||
123 | // There are three delta modes: |
||
124 | // * deltaMode 0 is by pixels, nothing to do |
||
125 | // * deltaMode 1 is by lines |
||
126 | // * deltaMode 2 is by pages |
||
127 | if ( orgEvent.deltaMode === 1 ) { |
||
128 | var lineHeight = $.data(this, 'mousewheel-line-height'); |
||
129 | delta *= lineHeight; |
||
130 | deltaY *= lineHeight; |
||
131 | deltaX *= lineHeight; |
||
132 | } else if ( orgEvent.deltaMode === 2 ) { |
||
133 | var pageHeight = $.data(this, 'mousewheel-page-height'); |
||
134 | delta *= pageHeight; |
||
135 | deltaY *= pageHeight; |
||
136 | deltaX *= pageHeight; |
||
137 | } |
||
138 | |||
139 | // Store lowest absolute delta to normalize the delta values |
||
140 | absDelta = Math.max( Math.abs(deltaY), Math.abs(deltaX) ); |
||
141 | |||
142 | if ( !lowestDelta || absDelta < lowestDelta ) { |
||
143 | lowestDelta = absDelta; |
||
144 | |||
145 | // Adjust older deltas if necessary |
||
146 | if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) { |
||
147 | lowestDelta /= 40; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | // Adjust older deltas if necessary |
||
152 | if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) { |
||
153 | // Divide all the things by 40! |
||
154 | delta /= 40; |
||
155 | deltaX /= 40; |
||
156 | deltaY /= 40; |
||
157 | } |
||
158 | |||
159 | // Get a whole, normalized value for the deltas |
||
160 | delta = Math[ delta >= 1 ? 'floor' : 'ceil' ](delta / lowestDelta); |
||
161 | deltaX = Math[ deltaX >= 1 ? 'floor' : 'ceil' ](deltaX / lowestDelta); |
||
162 | deltaY = Math[ deltaY >= 1 ? 'floor' : 'ceil' ](deltaY / lowestDelta); |
||
163 | |||
164 | // Add information to the event object |
||
165 | event.deltaX = deltaX; |
||
166 | event.deltaY = deltaY; |
||
167 | event.deltaFactor = lowestDelta; |
||
168 | // Go ahead and set deltaMode to 0 since we converted to pixels |
||
169 | // Although this is a little odd since we overwrite the deltaX/Y |
||
170 | // properties with normalized deltas. |
||
171 | event.deltaMode = 0; |
||
172 | |||
173 | // Add event and delta to the front of the arguments |
||
174 | args.unshift(event, delta, deltaX, deltaY); |
||
175 | |||
176 | // Clearout lowestDelta after sometime to better |
||
177 | // handle multiple device types that give different |
||
178 | // a different lowestDelta |
||
179 | // Ex: trackpad = 3 and mouse wheel = 120 |
||
180 | if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); } |
||
181 | nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200); |
||
182 | |||
183 | return ($.event.dispatch || $.event.handle).apply(this, args); |
||
184 | } |
||
185 | |||
202 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.